package com.gopi.utilities.action;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JMenuBar;
import javax.swing.SwingUtilities;

import com.gopi.utilities.common.Disposable;

abstract public class AbstractController implements Disposable
{
	protected Map<String, List<Component>> actionComponents;
	protected Map<String, Boolean> enableState;

	public AbstractController()
	{
		actionComponents = new HashMap<String, List<Component>>();
		enableState = new HashMap<String, Boolean>();
	}
	
	public void addActionCommand(String action, Component component) 
	{
		List<Component> components = actionComponents.get(action);
		if(components == null)
		{
			components = new ArrayList<Component>();
			actionComponents.put(action, components);
			Boolean b = enableState.get(action);
			if(b==null)
			{
				enableState.put(action,component.isEnabled());
			}
		}
		components.add(component);
	}
	
	public boolean getStatus(String action) 
	{
		Boolean b = enableState.get(action);
		return b==null ? false : b.booleanValue();
	}

	public void removeActionCommand(String action) 
	{
		List<Component> list = actionComponents.get(action);
		list.clear();
		actionComponents.remove(action);
		enableState.remove(action);
	}
	
	public void enableCommand(String action)
	{
		setEnable(action, true);
	}
	
	public void disableCommand(String action)
	{
		setEnable(action, false);
	}
	
	public void setEnable(String action, boolean enable) 
	{
		updateComps(action, enable, 1);
	}

	public void setVisible(String action, boolean enable) 
	{
		updateComps(action, enable, 2);
	}
	
	public void buildMenu(JMenuBar menuBar,MenuItemDescriptor[] desc,ActionListener listener)
	{
		setListener(desc,listener);
		for(MenuItemDescriptor des : desc)
		{
			menuBar.add(des.buildMenuUsingDescriptor(this));
		}
	}
	
	public void setListener(MenuItemDescriptor[] desc,ActionListener listener)
	{
		for(MenuItemDescriptor des : desc)
		{
			setListener(des,listener);
		}
	}
	
	public void setListener(MenuItemDescriptor desc,ActionListener listener)
	{
		desc.setMenuActionListener(listener);
		if(desc.getChildCount()>0)
		{
			setListener(desc.getChildren(),listener);
		}
	}
	
	protected void updateComps(final String action,final boolean enable,final int what)
	{
		final List<Component> list = actionComponents.get(action);
		if(list!=null)
		{
			enableState.put(action, enable);
			SwingUtilities.invokeLater(new Runnable() 
			{
				public void run()
				{
					for(Component component : list)
					{
						if(what==1)
							component.setEnabled(enable);
						else
							component.setVisible(enable);
					}
				}
			});
		}
	}	
	
	public void dispose()
	{
		enableState.clear();
		for(List l : actionComponents.values())
		{
			l.clear();
		}
		actionComponents.clear();
	}
	
}
